package action;

import dao.AddressDao;
import dao.OrderItemDao;
import po.Address;
import po.OrderItem;
import po.User;
import vo.OrderItemVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

@WebServlet(urlPatterns = "/user/action/OrderConfirmServlet")
public class OrderConfirmServlet extends HttpServlet {
    /**
     * 1.获取参数：OrderId
     * 2.从session中获取user
     * 3.创建地址对象
     * 4.根据user查询消费者的收货地址
     * 5.根据orderId查询订单货物和图书表中数据
     * 6.将地址和5中查询结果以及orderId绑定到request上
     * 7.转发到order-confirm.jsp页面
     */
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String orderId = req.getParameter("orderId");
        User user = (User)req.getSession().getAttribute("user");
        AddressDao addressDao = new AddressDao();
        ArrayList<Address> addresses = addressDao.selectByUser(user);
        OrderItemDao orderItemDao = new OrderItemDao();
        ArrayList<OrderItemVo> orderItems = orderItemDao.selectByIdAssociatedBook(orderId);
        req.setAttribute("orderItems",orderItems);
        req.setAttribute("addresses",addresses);
        req.setAttribute("orderId",orderId);
        req.getRequestDispatcher("../page/order-confirm.jsp").forward(req,resp);

    }
}
